package additional;

import static ogl.vecmathimp.FactoryDefault.vecmath;
import graph.parts.Shape.FaceForm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.vecmath.Vector2d;

import ogl.vecmath.Color;
import ogl.vecmath.Vector;

public class OBJLoader {
	private List<Vector> points = new ArrayList<Vector>();
	private List<Vector2d> texCoords = new ArrayList<Vector2d>();
	private List<Vector> normals = new ArrayList<Vector>();
	private List<Vertex> vertices = new ArrayList<Vertex>();
	private FaceForm ff = FaceForm.TRIANGLES;

	/*
	 * public static void main(String[] args) { String dir =
	 * System.getProperty("user.dir"); File f = new File(dir +
	 * "\\resources\\obj\\Cube.obj"); OBJLoader obj = new
	 * OBJLoader(f.getAbsolutePath()); }
	 */

	public OBJLoader(String path) {
		File f = new File(path);
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader(f));

			String line;
			while ((line = reader.readLine()) != null) {
				/* vertices */
				if (line.startsWith("v ")) {
					String[] vertice = line.split(" ");
					// vertice[0] wird nicht benötigt, enthält "v"
					float x;
					float y;
					float z;
					if (vertice[1].equals("")) {
						x = Float.valueOf(vertice[2]);
						y = Float.valueOf(vertice[3]);
						z = Float.valueOf(vertice[4]);
					} else {
						x = Float.valueOf(vertice[1]);
						y = Float.valueOf(vertice[2]);
						z = Float.valueOf(vertice[3]);
					}
					Vector p = vecmath.vector(x, y, z);
					points.add(p);
				}

				/* normals */
				if (line.startsWith("vn ")) {
					String[] vertice = line.split(" ");
					// vertice[0] wird nicht benötigt, enthält "vn"
					float x = Float.valueOf(vertice[1]);
					float y = Float.valueOf(vertice[2]);
					float z = Float.valueOf(vertice[3]);
					Vector n = vecmath.vector(x, y, z);
					normals.add(n);
				}

				/* texture */
				if (line.startsWith("vt ")) {
					String[] vertice = line.split(" ");
					float x = Float.valueOf(vertice[1]);
					float y = Float.valueOf(vertice[2]);
					Vector2d v = new Vector2d(x, y);
					texCoords.add(v);
				}

				/* faces */
				if (line.startsWith("f ")) {
					String[] face = line.split(" ");
					if (face.length == 5) {
						ff = FaceForm.QUADS;
					}
					for (int i = 1; i < face.length; i++) {
						String[] point = face[i].split("/");
						// Vertex erstellen mit Vektor Nr. point[0], Texture Coord
						// Nr. point[1] und Normale Nr. point[2]
						Vector vec = points.get(Integer.parseInt(point[0]) - 1);
						Vector2d tex = new Vector2d(1, 1);
						try {
							tex = texCoords.get(Integer.parseInt(point[1]) - 1);
						} catch (NumberFormatException e) {
							System.err.println("Keine Textur definiert");
						}
						Vector normal = vecmath.vector(0, 0, 0);
						if (point.length == 3) {
							normal = normals.get(Integer.parseInt(point[2]) - 1);
						}
						Vertex v = new Vertex(vec, normal, tex);
						vertices.add(v);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public FaceForm getFaceForm() {
		return ff;
	}

	public Vertex[] getVertices() {
		return vertices.toArray(new Vertex[vertices.size()]);
	}

}
